/* 
 * Copyright 2013 Sébastien Mouquet.
 * 
 * This file is part of ECovoiturage.
 * 
 * ECovoiturage is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * ECovoiturage is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with ECovoiturage. If not, see http://www.gnu.org/licenses/.
 */
package com.smouquet.ecovoiturage.ui.page;

import com.smouquet.ecovoiturage.core.bean.Config;
import com.smouquet.ecovoiturage.ui.NotificationUtil;
import com.smouquet.ecovoiturage.ui.Wizard;
import com.vaadin.data.Validator;
import com.vaadin.data.fieldgroup.FieldGroup;
import com.vaadin.data.fieldgroup.PropertyId;
import com.vaadin.data.util.BeanItem;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.Component;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;

/**
 * Etape d'installation de l'application : formulaire pour la configuration.
 * 
 * @author sebastien.mouquet
 */
public class InstallWizardConfigStep extends VerticalLayout implements Wizard.Step
{
    private Wizard wizard;
    
    private FormLayout layout;
    
    @PropertyId("jdbcDriver")
    private ComboBox cbDriver;
    @PropertyId("jdbcUrl")
    private TextField tfUrl;
    @PropertyId("jdbcUser")
    private TextField tfUser;
    @PropertyId("jdbcPassword")
    private TextField tfPassword;
    
    private FieldGroup fgForm;
    
    private Config config;
    
    
    public InstallWizardConfigStep()
    {
        layout();
    }

    
    /**
     * @return La configuration
     */
    public Config getConfig() {
        return config;
    }
    
    
    private void layout()
    {
        layout = new FormLayout();
        layout.setImmediate(true);
        
        cbDriver = new ComboBox("Driver JDBC");
        cbDriver.setWidth(300, Unit.PIXELS);
        cbDriver.setImmediate(true);
        cbDriver.setRequired(true);
        cbDriver.setValidationVisible(true);
        cbDriver.addItem("org.apache.derby.jdbc.ClientDriver");
        cbDriver.addItem("org.postgresql.Driver");
        
        tfUrl = new TextField("URL");
        tfUrl.setWidth(300, Unit.PIXELS);
        tfUrl.setImmediate(true);
        tfUrl.setRequired(true);
        tfUrl.setValidationVisible(true);
        tfUrl.setNullRepresentation("");
        tfUrl.setInputPrompt("jdbc:{derby|postgresql}://{host}:{1527|5432}/{database}");
        
        tfUser = new TextField("User");
        tfUser.setWidth(300, Unit.PIXELS);
        tfUser.setImmediate(true);
        tfUser.setRequired(true);
        tfUser.setValidationVisible(true);
        tfUser.setNullRepresentation("");
        
        tfPassword = new TextField("Password");
        tfPassword.setWidth(300, Unit.PIXELS);
        tfPassword.setImmediate(true);
        tfPassword.setRequired(true);
        tfPassword.setValidationVisible(true);
        tfPassword.setNullRepresentation("");
        
        layout.addComponent(cbDriver);
        layout.addComponent(tfUrl);
        layout.addComponent(tfUser);
        layout.addComponent(tfPassword);
        
        addComponent(layout);
        
        config = new Config();
        fgForm = new FieldGroup(new BeanItem<Config>(config));
        fgForm.bindMemberFields(this);
        
        setSizeFull();
        setMargin(true);
        setComponentAlignment(layout, Alignment.MIDDLE_CENTER);
    }

    
    @Override
    public void attachWizard(Wizard wizard)
    {
        this.wizard = wizard;
    }

    @Override
    public Component init()
    {
        wizard.setCaption("Installation - Paramètres d'accès à la base de données");
        wizard.setFinishEnabled(false);
        wizard.setCancelEnabled(false);
        
        return this;
    }

    @Override
    public void run() {}

    @Override
    public boolean onBeforePrevious() {
        return true;
    }

    @Override
    public void onAfterPrevious() {}

    @Override
    public boolean onBeforeNext()
    {
        try
        {
            fgForm.commit();
            
            return true;
            
        } catch(Validator.InvalidValueException ex) {
            NotificationUtil.showWarningNotification("Formulaire incomplet et/ou données invalides");
            return false;
        } catch(FieldGroup.CommitException ex) {
            NotificationUtil.showWarningNotification("Formulaire incomplet et/ou données invalides");
            return false;
        }
    }

    @Override
    public void onAfterNext() {}

    @Override
    public boolean onBeforeFinish() {
        return true;
    }

    @Override
    public void onAfterFinish() {}

    @Override
    public boolean onBeforeCancel() {
        return true;
    }

    @Override
    public void onAfterCancel() {}
}
